package com.swpu.select.service.impl;


import com.swpu.select.dao.CourseMapper;
import com.swpu.select.pojo.Course;
import com.swpu.select.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public int addCourse(Course course) {
        // 检查课程是否已存在
        if (courseMapper.getCourseById(course.getCourseId()) != null) {
            return 0;
        }
        return courseMapper.addCourse(course);
    }

    @Override
    public int updateCourse(Course course) {
        return courseMapper.updateCourse(course);
    }

    @Override
    public int deleteCourse(String courseId) {
        return courseMapper.deleteCourse(courseId);
    }
    @Override
    public List<Course> getCoursesByTeacher(@RequestParam String name) {
        return courseMapper.getCoursesByTeacher(name);
    }
    @Override
    public Course getCourseById(String courseId) {
        return courseMapper.getCourseById(courseId);
    }

    @Override
    public List<Course> getAllCourses() {
        return courseMapper.getAllCourses();
    }

    @Override
    public List<Course> getCoursesByDepartment(String department) {
        return courseMapper.getCoursesByDepartment(department);
    }

    @Override
    public List<Course> getCoursesByMajor(String major) {
        return courseMapper.getCoursesByMajor(major);
    }

    @Override
    public List<Course> getCoursesByCredit(int credit) {
        return courseMapper.getCoursesByCredit(credit);
    }

    @Override
    public int getCourseCount() {
        return courseMapper.getCourseCount();
    }

    @Override
    public double getAverageCredit() {
        List<Course> courses = getAllCourses();
        if (courses.isEmpty()) return 0.0;

        int totalCredit = 0;
        for (Course course : courses) {
            totalCredit += course.getCredit();
        }
        return (double) totalCredit / courses.size();
    }
}